/* eslint-disable @typescript-eslint/ban-ts-comment */
import React, { useEffect, useState } from 'react';
import Strings from '@/i18n';
import { useProps } from '@ray-js/panel-sdk';
import { publishDps, router, showToast, Text, View, getCustomConfig } from '@ray-js/ray';
import { NavBarCustom } from '@/components/NavBarCustom';
import { Cell, Checkbox, Switch, Dialog, DialogInstance, CellGroup } from '@ray-js/smart-ui';
import useHandleJump from '@/hooks/useHandleJump';
import clsx from 'clsx';
import styles from './index.module.less';
import { SwitchWihtLoading } from '@/components/groupInfo';

export const decodeArea = (area: string) => {
  // 将十六进制字符串转换为8位二进制字符串
  const binary = parseInt(area, 16).toString(2).padStart(8, '0');
  return binary.split('').reverse().join('');
};

// 添加encodeArea方法，将8位二进制字符串转换为十六进制字符串
export const encodeArea = (binary: string) => {
  // 将二进制字符串转换为2位十六进制字符串
  return parseInt(binary, 2).toString(16).padStart(2, '0');
};

const Setting: React.FC = () => {
  const { smart_switch: smartSwitch } = useProps();

  const handleJump = useHandleJump();

  const [password, setPassword] = useState('12345678');

  useEffect(() => {
    getCustomConfig({
      success: res => {
        console.log(`🚀 ~ res:`, res);
        setPassword(res.password || '12345678');
      },
      fail: err => {
        console.error('获取配置失败:', err);
      },
    });
  }, []);

  const beforeClose = (
    action: 'cancel' | 'confirm' | 'overlay',
    value?: string
  ): Promise<boolean> =>
    new Promise(resolve => {
      setTimeout(() => {
        if (action === 'confirm') {
          // 修改这里，传递新的 value 值
          // onChange && onChange(value || '');
          if (value === password) {
            resolve(true);

            setTimeout(() => {
              handleJump('drugSet');
            }, 100);
          } else {
            showToast({
              title: '密码错误',
              icon: 'error',
            });
            resolve(false);
          }
        } else {
          // 拦截取消操作
          resolve(true);
        }
      }, 10);
    });

  return (
    <View className={styles.view}>
      <NavBarCustom name={Strings.getLang('setting')} back />
      <View className={styles.padding} />

      <CellGroup inset customClass={styles.cellGroup} className={styles.cellGroup}>
        <Cell title={Strings.getLang('smart_switch')}>
          <SwitchWihtLoading
            checked={smartSwitch}
            onChange={value => {
              publishDps({ smart_switch: !smartSwitch });
            }}
          />
        </Cell>

        <Cell
          title={Strings.getLang('manager')}
          isLink
          label="默认密码12345678"
          onClick={() => {
            DialogInstance.input({
              selector: '#smart-dialog-password',
              title: '请输入密码',
              value: '',
              beforeClose,
              cancelButtonText: Strings.getLang('cancel'),
              confirmButtonText: Strings.getLang('confirm'),
            });
          }}
        />
      </CellGroup>
      <Dialog id="smart-dialog-password" />
    </View>
  );
};

export default Setting;
